/**  
* @Title: ShopGoodsController.java
* @Package com.lemon.controller.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月23日
* @version V1.0  
*/
package com.lemon.controller.cityRun;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.conf.MyInterceptor.LoginRequired;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.model.cityRun.CrShopGoods;
import com.lemon.model.cityRun.CrShopGoodsSpec;
import com.lemon.model.cityRun.CrShopGoodsType;
import com.lemon.service.cityRun.CrShopGoodsService;
import com.lemon.utils.ReqResult;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: ShopGoodsController</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月23日
 * @版本 V 1.0 
 */
@RestController
@RequestMapping("/goods")
public class ShopGoodsController {
	@Resource
	private CrShopGoodsService goService;

	////////////////////////////商品信息///////////////////////////////////
	/**
	 * <b>Description:<blockquote> 查询商品列表
	 * </blockquote></b>
	 * 
	 * @Title: getGoodsList
	 * @param go
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@RequestMapping("/getGoodsList")
	public ReqResult getGoodsList(@ModelAttribute CrShopGoods go , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			if (StringUtil.isEmpty(go.getGoodsTypeId())) {
				go.setGoodsTypeId(null);
			}
			go.setWhere(" and a.status != 0 ");
			go.setOrderBy(" order by a.sort asc");
			List<CrShopGoods> list = goService.selectListBySelective(go);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 根据id 查询商品信息
	 * </blockquote></b>
	 * 
	 * @Title: getGoodsById
	 * @param go
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@RequestMapping("/getGoodsById")
	public ReqResult getGoodsById(@ModelAttribute CrShopGoods go , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			List<CrShopGoods> list = goService.selectListBySelective(go);
			CrShopGoods ggo = list.size() > 0 ? list.get(0) : null;
			if (ggo != null) {
				CrShopGoodsSpec se = new CrShopGoodsSpec();
				se.setGoodsId(ggo.getId());
				se.setWhere(" and a.status != 0 ");
				List<CrShopGoodsSpec> list2 = goService.selectSpecListBySelective(se);
				ggo.setSpecList(list2);
			}
			reqResult.setReturnResult(ggo);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 添加商品信息
	 * </blockquote></b>
	 * 
	 * @Title: addGoodsInfo
	 * @param gt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@LoginRequired
	@RequestMapping("/addGoodsInfo.do")
	public ReqResult addGoodsInfo(@ModelAttribute CrShopGoods gt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			//			if (StringUtil.isNotBlank(gt.getPriceOutStr())) {
			//				String outStr = gt.getPriceOutStr();
			//				double d = Double.parseDouble(outStr);
			//				gt.setPriceOut(Integer.parseInt(d * 100));
			//			}
			//			if (StringUtil.isNotBlank(gt.getPriceRoomStr())) {
			//				gt.setPriceRoom(gt.getPriceRoom() * 100);
			//			}
			//			if (StringUtil.isNotBlank(gt.getGoodsPackAmountStr())) {
			//				gt.setGoodsPackAmount(gt.getGoodsPackAmount() * 100);
			//			}
			int list = goService.insertBySelective(gt);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改商品信息
	 * </blockquote></b>
	 * 
	 * @Title: updateGoodsInfo
	 * @param go
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@LoginRequired
	@RequestMapping("/updateGoodsInfo.do")
	public ReqResult updateGoodsInfo(@ModelAttribute CrShopGoods go , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			String ids = request.getParameter("ids");
			String specids    = request.getParameter("specids");
			String spectits   = request.getParameter("spectits");
			String specromss  = request.getParameter("specromss");
			String speckucs   = request.getParameter("speckucs");
			String specbzs    = request.getParameter("specbzs");
			String specouts   = request.getParameter("specouts");

			String shopId = go.getShopId();

			/*
			 * if (StringUtil.isNotBlank(specids)) {
			 * String[] split = specids.split("-");
			 * CrShopGoodsSpec se = new CrShopGoodsSpec();
			 * se.setGoodsId(go.getId());
			 * se.setWhere(" and a.status != 0 ");
			 * List<CrShopGoodsSpec> list2 = goService.selectSpecListBySelective(se);
			 * for (int i = 0; i < list2.size(); i++) {
			 * CrShopGoodsSpec spec = list2.get(i);
			 * for (int j = 0; j < split.length; j++) {
			 * if (!"0".equals(split[i])) {
			 * }
			 * }
			 * }
			 * }
			 */
			CrShopGoodsSpec se = new CrShopGoodsSpec();
			se.setGoodsId(go.getId());
			se.setStatus(0);
			goService.updateByPrimaryKeySelectiveByGoodsId(se);
			if (StringUtil.isNotBlank(spectits)) {
				String[] split = spectits.split("-");
				for (int i = 0; i < split.length; i++) {
					String idss = specids.split("-")[i];
					String titls = split[i];
					String mss = specromss.split("-")[i];
					String kucs = speckucs.split("-")[i];
					String bzs = specbzs.split("-")[i];
					String outs = specouts.split("-")[i];

					se = new CrShopGoodsSpec();
					se.setTitle(titls);
					se.setPriceOut(Integer.parseInt(outs));
					se.setPriceRoom(Integer.parseInt(mss));
					se.setGoodsPackAmount(Integer.parseInt(bzs));
					se.setGoodsStock(Integer.parseInt(kucs));
					se.setId(idss);
					if (!idss.equals("0")) {
						//xiugai
						goService.updateSpecBySelective(se);
					} else {
						se.setStatus(1);
						//tianjia
						goService.insertSpecBySelective(se);
					}
					                      
				}
			}

			if (StringUtil.isNotBlank(ids)) {
				String[] split = ids.split(",");
				for (int i = 0; i < split.length; i++) {
					String id = split[i];
					go.setId(id);
					goService.updateBySelective(go);
				}
				reqResult.setReturnResult("1");
				return reqResult;
			}

			int list = goService.updateBySelective(go);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	//商品分类//////////////////////////////////////////////////////

	/**
	 * <b>Description:<blockquote> 获取分类列表
	 * </blockquote></b>
	 * 
	 * @Title: getGoodsTypeList
	 * @param gt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@RequestMapping("/getGoodsTypeList")
	public ReqResult getGoodsTypeList(@ModelAttribute CrShopGoodsType gt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			gt.setWhere(" and status != 0 ");
			gt.setOrderBy(" order by sort asc");
			List<CrShopGoodsType> list = goService.selectTypeListBySelective(gt);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 根据id获取分类列表
	 * </blockquote></b>
	 * 
	 * @Title: getGoodsTypeListById
	 * @param gt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@RequestMapping("/getGoodsTypeById")
	public ReqResult getGoodsTypeById(@ModelAttribute CrShopGoodsType gt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			List<CrShopGoodsType> list = goService.selectTypeListBySelective(gt);
			reqResult.setReturnResult(list.size() > 0 ? list.get(0) : null);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 添加商品分类
	 * </blockquote></b>
	 * 
	 * @Title: addGoodsTypeList
	 * @param gt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@LoginRequired
	@RequestMapping("/addGoodsTypeInfo.do")
	public ReqResult addGoodsTypeList(@ModelAttribute CrShopGoodsType gt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {

			gt.setStatus(1);
			int list = goService.insertTypeBySelective(gt);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 修改商品分类
	 * </blockquote></b>
	 * 
	 * @Title: updateGoodsTypeList
	 * @param gt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@LoginRequired
	@RequestMapping("/updateGoodsTypeInfo.do")
	public ReqResult updateGoodsTypeList(@ModelAttribute CrShopGoodsType gt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			int list = goService.updateTypeBySelective(gt);
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	/**
	 * <b>Description:<blockquote> 统计商品分类下面有多少商品
	 * </blockquote></b>
	 * 
	 * @Title: updateGoodsTypeList
	 * @param gt
	 * @param request
	 * @param response
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@LoginRequired
	@RequestMapping("/selectGoodsCountByTypeId.do")
	public ReqResult selectGoodsCountByTypeId(@ModelAttribute CrShopGoodsType gt , HttpServletRequest request ,
			HttpServletResponse response) {
		ReqResult reqResult = new ReqResult();
		try {
			List<Map<String , Object>> list = goService.selectGoodsCountByTypeId(gt);
			for (int i = 0; i < list.size(); i++) {
				Map<String , Object> map = list.get(i);
				Object integer = map.get("typeId");
				map.put("typeId" , IdTypeHandler.encode(Long.parseLong(integer.toString())));
			}
			reqResult.setReturnResult(list);
		} catch (Exception e) {
			MyException.writeErr(e , "");
		}
		return reqResult;
	}

	//////////////////////菜品规格///////////////////////
	/*
	 * @RequestMapping("/getGoodsSpecList")
	 * public ReqResult getGoodsSpecList(@ModelAttribute CrShopGoodsSpec spec , HttpServletRequest request ,
	 * HttpServletResponse response) {
	 * ReqResult reqResult = new ReqResult();
	 * try {
	 * spec.setWhere(" and status != 0 ");
	 * List<CrShopGoodsSpec> list = goService.selectSpecListBySelective(spec);
	 * reqResult.setReturnResult(list);
	 * } catch (Exception e) {
	 * MyException.writeErr(e , "");
	 * }
	 * return reqResult;
	 * }
	 */

	public static void main(String[] args) {
		String uu = "b8qp-mhaa-0-0-0";
		for (int i = 0; i < uu.split("-").length; i++) {
			System.out.println((i + 1) + "=" + uu.split("-")[i]);
		}
	}

}
